package com.be.beadmin.basicData.repository;


import com.be.beadmin.basicData.dto.output.UserNameId;
import com.be.beadmin.basicData.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public interface UserRepository extends JpaRepository<User,String>, JpaSpecificationExecutor<User> {
    User findUserByNameAndDelete(String name, Integer delete);
    User findUserByIdAndDelete(String id,Integer delete);
    List<User> findUserByRolesAndDelete(String name, Integer delete);

    List<User> findUsersByDelete(Integer delete);

//    @Query(value = "select su.name from sys_user su where su.id = ?",nativeQuery = true)
//    String findNamesByUserId(String userId);

    @Query(value = "select sur.user_id from sys_user_role sur where sur.role_id = ?",nativeQuery = true)
    List<String> findUserIdsByRoleId(String roleId);

    /**
     * 3、根据项目经理查询是否存在该人员
     */
    boolean findUserByName(String name);

    /**
     * 根据项目经理名称查询对应的权限id
     * @param name
     * @return
     */
    String findIdByName(String name);

    @Query(value = "select su.name from sys_user su where su.id = ?",nativeQuery = true)
    String findUserNameById(String userId);

    @Query(value = "select new com.be.beadmin.basicData.dto.output.UserNameId(u.id, u.name)  from User u")
    List<UserNameId> findAllUser();
}
